using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Inventory.Domain.Models;
using Inventory.Database;

namespace Inventory.Database.Models
{ 
    public class MovieRepository : IMovieRepository
    {
        private readonly InventoryContext _context;

        public MovieRepository(UnitOfWork unitOfWork )
        {
            _context = unitOfWork.Context;
        }

        public IQueryable<Movie> All
        {
            get { return _context.Movies; }
        }

        public IQueryable<Movie> AllIncluding(params Expression<Func<Movie, object>>[] includeProperties)
        {
            IQueryable<Movie> query = _context.Movies;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Movie Find(int id)
        {
            return _context.Movies.Find(id);
        }

        public void InsertOrUpdate(Movie movie)
        {
            if (movie.Id == default(int)) {
                // New entity
                _context.Movies.Add(movie);
            } else {
                // Existing entity
                _context.Entry(movie).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var movie = _context.Movies.Find(id);
            _context.Movies.Remove(movie);
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        public void Dispose() 
        {
            _context.Dispose();
        }
    }

    
}